package yxy.cherry.battle.module.affix.skill;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.AffixFactory;
import yxy.cherry.battle.module.affix.AffixFactory.BuffTypeEnum;
import yxy.cherry.battle.module.affix.AffixFactory.SkillTypeEnum;
import yxy.cherry.battle.module.affix.Buff;
import yxy.cherry.battle.module.affix.Skill;
import yxy.cherry.battle.module.affix.SkillType;
import yxy.cherry.battle.module.affix.condition.AffixCondition;
import yxy.cherry.battle.module.record.stream.RecordStreamGroup;
import yxy.cherry.battle.module.record.stream.RecordStream_01_附加效果;
import yxy.cherry.battle.module.record.stream.RecordStream_SKL;
import yxy.cherry.data.bean.skill;

/** 附加效果 */
@SkillType(Type = SkillTypeEnum.BUFF)
public class Affix_01_附加效果 extends Skill {

	public Affix_01_附加效果(Fighter actor, skill skilldata, int probability, int[] params) {
		super(actor, skilldata, probability, params);
	}

	private RecordStreamGroup steamGroup;

	@Override
	public RecordStreamGroup record() {
		steamGroup = getRecord().stream(new RecordStreamGroup());
		steamGroup.add(new RecordStream_SKL(getActor(), getSkillId()));
		return steamGroup;
	}

	@Override
	protected boolean _exec(Fighter actor, Fighter target) {
		for (AffixCondition affixCondition : getConditionSet()) {
			boolean cond = affixCondition.check(actor, target);
			if (!cond) {
				return false;
			}
		}
		return exec(actor, target);
	}

	@Override
	public boolean exec(Fighter actor, Fighter target) {
		if (!target.is活着()) {
			return false;
		}

		BuffTypeEnum type = BuffTypeEnum.valueOf(getSkilldata().getBufftype());
		if (type == null) {
			return false;
		}

		Buff buff = AffixFactory.getInstance().getBuff(type, actor, target, getSkilldata(), getParams());

		if (buff == null) {
			return false;
		}
		try {
			boolean result = false;
			switch (buff.getBuffType()) {
			case 控制效果: {
				if (!is控制(actor, target, getProbability())) {
					return false;
				}
			}
				break;
			default: {
				if (!isProb()) {
					return false;
				}
			}
				break;
			}
			result = buff.append(target);
			if (!result) {
				return false;
			}

			// 流脚本
			RecordStream_01_附加效果 recordSteam = steamGroup.add(new RecordStream_01_附加效果(target));
			recordSteam.addBuff(buff);
			recordSteam.record();
			Battle.battleLogger(6,"附加效果："+actor.getName()+"给"+target.getName()+"附加了"+this+ ")[" + getSkilldata().getBuffid() + buff + "]"+"唯一ID："+buff.getBuffCodeSeq()+"叠加层数："+target.getBuffMap().get(buff.getBuffCodeSeq()).getOverlay()+"最大层数："+buff.getOverlayMax());

			return true;
		} catch (Exception e) {
			return false;
		}
	}

}